﻿using Manage.Traceability.Domain;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace SqlSUger
{
    public static class SqlSugerExt
    {
        public static IServiceCollection Addscop(
           this IServiceCollection services, WebApplicationBuilder builser)
        {
            //Scoped用SqlSugarClient 
            SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
            {

                DbType = SqlSugar.DbType.MySql,
                ConnectionString = builser.Configuration["ConnectionStrings:Strcon"],
                IsAutoCloseConnection = true,
            },
           db =>
           {
               //每次上下文都会执行

               //获取IOC对象不要求在一个上下文
               //var log=s.GetService<Log>()

               //获取IOC对象要求在一个上下文
               //var appServive = s.GetService<IHttpContextAccessor>();
               //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();
               //建库,有库不创建,没库才创建
               db.DbMaintenance.CreateDatabase();
               //创建表
               db.CodeFirst.InitTables<ShopModels, SourceModels, FarmingModel, FarmingCompanyModel, ImgsModel>();
               db.CodeFirst.InitTables<SlaughterModel, HealthModel, ProvinceModel, CityModel, CountyModel>();
               db.CodeFirst.InitTables<SlaugModel>();
               db.Aop.OnLogExecuting = (sql, pars) =>
               {
                   Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
               };
           });
            return services;
        }
    }
}